﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProfileSectorActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The profile sector actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Profiles
{
    using System;

    using NHibernate;

    using ServiceLibrary.Models.Base;
    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The profile sector actioner.
    /// </summary>
    public class ProfileSectorActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProfileSectorActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The session.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public ProfileSectorActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create sector.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreateSector(string name)
        {
            var sector = new ProfileSector
                             {
                                 ID = Guid.NewGuid(), 
                                 Name = name, 
                                 CreatedDateTime = DateTime.Now
                             };
            this.Session.Save(sector);

            this.Log.CreateLogEntry(
                sector.ID, string.Format("Sektor '{0}', blev oprettet.", sector.Name), Enums.LogEntryType.ProfileSector);

            return sector.ID;
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileSectorsResult"/>.
        /// </returns>
        public ProfileSectorsResult ExecuteSearchQuery(ProfileSectorsQuery query)
        {
            SearchSettings settings = query.Settings;

            var result = new ProfileSectorsResult();

            IQueryOver<ProfileSector, ProfileSector> search = this.Session.QueryOver<ProfileSector>();
            this.ApplyDefaultCriterias(settings, search);
            
            result.TotalRowCount = search.RowCount();

            var sectors = this.ApplySkipAndTake(settings, search.OrderBy(x => x.Name).Asc);
            
            foreach (ProfileSector profileSector in sectors)
            {
                Services.Dto.ProfileSector dto = Services.Dto.ProfileSector.Convert(profileSector);
                if (dto != null)
                {
                    result.List.Add(dto);
                }
            }

            return result;
        }

        /// <summary>
        /// The get sector.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileSector"/>.
        /// </returns>
        public ProfileSector GetSector(Guid id)
        {
            return this.Session.Get<ProfileSector>(id);
        }

        #endregion
    }
}